package com.xp.flower.qsd.utils.weixin.pay.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.scripting.xmltags.WhereSqlNode;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.xp.flower.qsd.utils.MD5Util;
import com.xp.flower.qsd.utils.weixin.WeiXinGlobalConstants;

@SuppressWarnings("all")
public class WXUtils {
    /**
     * MD5加密
     * @param characterEncoding
     * @param parameters
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
    StringBuffer sb = new StringBuffer();
    Set es = parameters.entrySet();
    Iterator it = es.iterator();
    while(it.hasNext()) {
        Map.Entry entry = (Map.Entry)it.next();
        String k = (String)entry.getKey();
        Object v = entry.getValue();
        if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
            sb.append(k + "=" + v + "&");
        }
    }
    sb.append("key=" + WeiXinGlobalConstants.getProperties("mchId"));
    //微信规定签名必须都为大写
    String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
    return sign;
}
    
    @SuppressWarnings("unchecked")
    public static String createSign(String characterEncoding,JSONObject json) throws JSONException{

        StringBuilder sb = new StringBuilder();
        Set<String> keySet = json.keySet();
        Iterator<String> it = keySet.iterator();
        
        while(it.hasNext()){
            String k = (String) it.next();
            String v = json.getString(k);
            if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
                   sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + WeiXinGlobalConstants.getProperties("mchId"));//MCH_KEY
        String s = sb.toString();
        System.out.println(s);
        //微信规定签名必须都为大写
        String sign = MD5Util.MD5Encode(s, characterEncoding).toUpperCase();
        return sign;
    }
    
    /**
     * 创建32随机数
     * @return
     */
    public static String CreateNoncestr(int num){
            StringBuilder sb = new StringBuilder();
            Random r = new Random();
            for(int i=0; i<num; i++){
                    sb.append(r.nextInt(9));
            }
            return sb.toString();
    }
    
    /**
     * 根据request获取ip
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request){
            String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        } 
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }
    
    /**
     * 变成一个XML文件
     * @param return_code
     * @param return_msg
     * @return
     */
    public static String setXML(String return_code, String return_msg) {
            return "<xml><return_code><![CDATA[" + return_code
            + "]]></return_code><return_msg><![CDATA[" + return_msg
            + "]]></return_msg></xml>";
    }
    
    //微信统一下单请求
    public static String httpsRequest(String requestUrl, String requestMethod, String outputStr){
            try {
                    URL url = new URL(requestUrl);
                    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    // 设置请求方式（GET/POST）
                    conn.setRequestMethod(requestMethod);
                    conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
                    // 当outputStr不为null时向输出流写数据
                    if (null != outputStr) {
                            OutputStream outputStream = conn.getOutputStream();
                            // 注意编码格式
                            outputStream.write(outputStr.getBytes("UTF-8"));
                            outputStream.close();
                    }
                    // 从输入流读取返回内容
                    InputStream inputStream = conn.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    String str = null;
                    StringBuffer buffer = new StringBuffer();
                    while ((str = bufferedReader.readLine()) != null) {
                            buffer.append(str);
                    }
                    // 释放资源
                    bufferedReader.close();
                    inputStreamReader.close();
                    inputStream.close();
                    inputStream = null;
                    conn.disconnect();
                    return buffer.toString();
            } catch (ConnectException ce) {
                    System.out.println("连接超时：{}");
            } catch (Exception e) {
                    System.out.println("https请求异常：{}");
            }
            return null;
    }
}
